package com.kth.subtranslator.ui.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComponent;

import org.divxdede.swing.busy.JBusyComponent;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.kth.subtranslator.IBatchTranslateCallBack;
import com.kth.subtranslator.ITranslateDriver;
import com.kth.subtranslator.LanguageNotSupportedException;
import com.kth.subtranslator.SrtEntry;
import com.kth.subtranslator.SrtParser;
import com.kth.subtranslator.TranslateEngine;
import com.kth.subtranslator.TranslateEngine.Language;
import com.kth.subtranslator.evt.EndTranslateEvent;
import com.kth.subtranslator.evt.LanguageSelectedEvent;
import com.kth.subtranslator.evt.StartTranslateEvent;
import com.kth.subtranslator.evt.StopTranslateEvent;
import com.kth.subtranslator.ui.SrtPanel;

public class SrtPanelController {

	private final SrtPanel view = new SrtPanel();
	private final JBusyComponent<SrtPanel> busy = new JBusyComponent<SrtPanel>(view);
	private File sourceFile;
	private File targetFile;
	private final TranslateEngine engine = new TranslateEngine();

	private Language sourceLanguage = Language.ENGLISH;
	private EventBus eventBus;

	public SrtPanelController(EventBus eventBus) {
		this.eventBus = eventBus;
		eventBus.register(this);
	}

	public JComponent getView() {
		return busy;
	}

	public void setSourceEntries(List<SrtEntry> srtEntries) {
		view.setSourceEntries(srtEntries);
	}

	public void setSourceFile(File sourceFile) {
		this.sourceFile = sourceFile;
	}

	public File getSourceFile() {
		return sourceFile;
	}

	public void addSourceEntries(List<SrtEntry> srtEntries) {
		view.addSourceEntries(srtEntries);
	}

	public void addTargetEntries(List<SrtEntry> srtEntries) {
		view.addTargetEntries(srtEntries);
	}
	
	public void setTargetEntries(List<SrtEntry> srtEntries) {
		view.setTargetEntries(srtEntries);
	}

	public void setTargetFile(File targetFile) {
		this.targetFile = targetFile;
	}

	public File getTargetFile() {
		return targetFile;
	}

	public List<SrtEntry> getTargetEntries() {
		return view.getTargetEntries();
	}

	public List<SrtEntry> getSourceEntries() {
		return view.getSourceEntries();
	}
	
	public void clearTargetEntries() {
		view.clearTargetEntries();
	}

	public void saveTarget(String absoluteFilePath) {
		this.targetFile = new File(absoluteFilePath);
		String fileContent = new SrtParser().parse(view.getTargetEntries());
		Writer out;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(absoluteFilePath), "UTF-8"));
			try {
				out.write(fileContent);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					out.close();
					System.out.println("Target Subtitle saved at : " + absoluteFilePath);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}	
			//				}
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}

	public List<SrtEntry> translate(ITranslateDriver driver, List<SrtEntry> entries, Language toLang, final IBatchTranslateCallBack callBack) {
		List<String> txt = new ArrayList<String>();
		for (SrtEntry srtEntry : entries) {
			txt.add(srtEntry.getContent());
		}

		Collection<String> translatedTxt;
		List<SrtEntry> translatedEntries = new ArrayList<SrtEntry>();
		try {
			translatedTxt = engine.batchTranslate(driver, txt, getSourceLanguage(), toLang, callBack);
			int i = 0;
			for (Iterator<String> iterator = translatedTxt.iterator(); iterator
					.hasNext();) {
				String srtEntry = iterator.next();
				translatedEntries.add( new SrtEntry(i, entries.get(i).getStartTime(), entries.get(i).getEndTime(), srtEntry) );
				i++;
			}
		} catch (LanguageNotSupportedException e) {
			e.printStackTrace();
		}
		return translatedEntries;
	}

	public Collection<Language> getSupportedLanguage() {
		return engine.getSupportedLanguages();
	}

	public ITranslateDriver getDefaultDriver() {
		return engine.getDefaultDriver();
	}

	public Collection<ITranslateDriver> getDrivers() {
		return engine.getDrivers();
	}

	@Subscribe
	public void setSourceLanguage(LanguageSelectedEvent evt) {
		this.sourceLanguage = evt.getLanguage();
	}

	/**
	 * @return the sourceLanguage
	 */
	public Language getSourceLanguage() {
		return sourceLanguage;
	}
	
	@Subscribe
	public void stopEngine(StopTranslateEvent evt) {
		System.out.println("event : " + evt);
		engine.stop();
	}
	
//	@Subscribe
//	public void startTranslation(StartTranslateEvent evt) {
//		busy.setBusy(true);
//	}
//	
//	@Subscribe
//	public void stopTranslation(EndTranslateEvent evt) {
//		busy.setBusy(false);
//	}

}
